package io.github.cameradddd.entity;

import java.time.LocalDateTime;

import io.github.cameradddd.dao.AccountDAO;

import lombok.Data;

/**
 * 这是一个账目记录类
 * 存储了创建时间,金额等消息
 */
@Data
public class AccountRecord {
    private int id;
    private LocalDateTime createdDate;
    private boolean income = false;// 默认支出
    private double amount;
    private String remark;// 备注信息

    /**
     * 自动创建日期的构造方法,同时自动分配id
     * @param income
     * @param amount
     * @param remark
     */
    public AccountRecord(boolean isIncome, double amount, String remark) {
        this.amount = amount;
        this.income = isIncome;
        if (remark != null) {
            this.remark = remark.replace(',', '，');
        } else {
            this.remark = null;
        }
        this.createdDate = LocalDateTime.now();
        this.id = AccountDAO.newId();
    }

    /**
     * 手动指定日期和id的构造方法
     * 
     * @param createdDate
     * @param income
     * @param amount
     * @param msg
     */
    public AccountRecord(int id, LocalDateTime createdDate, boolean income, double amount, String remark) {
        this.createdDate = createdDate;
        this.amount = amount;
        this.income = income;

        this.id = id;
        if (remark != null) {
            this.remark = remark.replace(',', '，');
        } else {
            this.remark = null;
        }
    }

    public void setRemark(String remark) {
        if (remark != null) {
            this.remark = remark.replace(',', '，');
        } else {
            this.remark = null;
        }
    }

    /**
     * 将record实例转化为字符串写入data.csv
     */
    public void writeToDisk() {
        AccountDAO.save(this);
    }

    @Override
    public String toString() {

        return id + "," + this.createdDate + "," +
                ((this.income == true) ? "收入" : "支出") + "," +
                this.amount + "," + this.remark + "\n";
    }
}
